<meta charset="UTF-8"/>
<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Login extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
    }

    function dangnhap() {
        $email = $this->input->post('username_login');
        $pass = md5($this->input->post('pass_login'));
        echo $email; echo $pass;
        
        $this->load->model("mtaikhoan");
        $tk = new Mtaikhoan();
        if ($tk->dangnhap($email, $pass)) {
            echo "OK";
            header("location:" . base_url("index.php"));
        } else {
            echo "KHONG DUNG";
            header("location:" . base_url("index.php/welcome/vlogin"));
        }
    }

    function dangky() {
        $this->load->model("mtaikhoan");
        $tk = new Mtaikhoan();
        /////////////
        $matk = $tk->get_top_id();
        $name = $this->input->post('name');
        $tentk = $this->input->post('tentk');
        $password = md5($this->input->post('password'));
        $email = $this->input->post('email');
        $date = $this->input->post('date');
        $sex = $this->input->post('gioitinh');
        /////////check info
        ///check tentk
        $checktentk = $tk->getObject("tentk", $tentk);
        $checkemail = $tk->getObject("email", $email);
        if ($checktentk) {
            echo"<script>";
            echo 'alert("Tên Tài khoản đã tồn tại")';
            echo 'window.location ="';
            echo base_url();
            echo '"';
            echo"</script>";
            //redirect(base_url("index.php/login/dangky"));
        } else if ($checkemail) {
            echo"<script>";
            echo 'alert("Email đã được đăng ký")';
            echo"</script>";
        } else {

            /*
              ////////
              echo $matk;
              echo $tentk;
              echo $email;
              echo $password;
              echo $date;
              echo $sex;
              echo $name;
              ////////
             * */

            $tk->set_matk($matk);
            $tk->set_tentk($tentk);
            $tk->set_matkhau($password);
            $tk->set_email($email);
            $tk->set_loaitk("3");
            $tk->set_ngaysinh($date);
            $tk->set_gioitinh($sex);
            $tk->set_tenkh($name);

            ////
            $tk->Save();
            redirect(base_url());
        }
    }

    function thoat() {
        $this->session->sess_destroy();
        header("location:" . base_url("index.php"));
    }

}
?>
